package com.wf.ew.daily.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.DateUtil;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.daily.biz.DiaryBiz;
import com.wf.ew.daily.model.Diary;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-01-05 11:24:24
 * @version ：1.0
 */
@Repository
public class DiaryBizImpl extends BaseDao implements DiaryBiz {

	@Override
	public String list(PageResult<T> page, Diary diary, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Diary> rm = new BeanPropertyRowMapper<Diary>(Diary.class);
		StringBuffer sql = new StringBuffer("select d.id,d.companyId,d.playTime");
        sql.append(",d.place,d.people,d.type,d.state");
		sql.append(",d.content,d.createTime,d.createUser,d.updateTime");
		sql.append(",d.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_diary d ");
        sql.append(" inner join sys_user suser on suser.id=d.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=d.companyId ");
        sql.append(" where d.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and d.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(diary)){
//			if(Utils.isNotEmpty(diary.getName())){
//				sql.append(" and instr(d.name,:name)>0 ");
//				map.put("name", diary.getName());
//			}
			if(Utils.isNotEmpty(diary.getSort())&&Utils.isNotEmpty(diary.getOrder())){
				sql.append(" order by "+diary.getSort()+" "+diary.getOrder());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Diary> list(Diary diary, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Diary> rm = new BeanPropertyRowMapper<Diary>(Diary.class);
        StringBuffer sql = new StringBuffer("select d.id,d.companyId,d.playTime");
        sql.append(",d.place,d.people,d.type,d.state");
		sql.append(",d.content,d.createTime,d.createUser,d.updateTime");
		sql.append(",d.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_diary d ");
        sql.append(" inner join sys_user suser on suser.id=d.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=d.companyId ");
        sql.append(" where d.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and d.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(diary)){
//			if(Utils.isNotEmpty(diary.getName())){
//				sql.append(" and instr(d.name,:name)>0 ");
//				map.put("name", diary.getName());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Diary getById(Diary diary) {
		RowMapper<Diary> rm = new BeanPropertyRowMapper<Diary>(Diary.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id,d.companyId,d.playTime");
        sql.append(",d.place,d.people,d.type,d.state");
		sql.append(",d.content,d.createTime,d.createUser,d.updateTime");
		sql.append(",d.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_diary d ");
        sql.append(" inner join sys_user suser on suser.id=d.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=d.companyId ");
        sql.append(" where d.del=0 and cmy.del=0 and d.id=:id ");
		map.put("id", diary.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(Diary diary, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_diary (");
        sql.append("companyId,playTime,place,people");
		sql.append(",type,state,content,createUser) values ( ");
        sql.append(":companyId,:playTime,:place,:people");
		sql.append(",:type,:state,:content,:createUser)");
        map.put("companyId", loginUser.getCompanyId());
        map.put("playTime", diary.getPlayTime());
        map.put("place", diary.getPlace());
        map.put("people", diary.getPeople());
        map.put("type", diary.getType());
        map.put("state", diary.getState());
        map.put("content", diary.getContent());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Diary diary, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_diary set ");
        sql.append("playTime=:playTime,place=:place,people=:people ");
		sql.append(",type=:type,state=:state,content=:content ");
		sql.append(",updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("playTime", diary.getPlayTime());
        map.put("place", diary.getPlace());
        map.put("people", diary.getPeople());
        map.put("type", diary.getType());
        map.put("state", diary.getState());
        map.put("content", diary.getContent());
		map.put("updateUser", loginUser.getId());
		map.put("id", diary.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Diary diary, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(diary.getIds().length()>0){
			for(String ID : diary.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_diary set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
